Support explicit 0 and 1 forms for plural in PHP
authorNiklas Laxström <niklas.laxstrom@gmail.com>
Sun, 16 Sep 2012 19:13:03 +0000 (19:13 +0000)
committerNiklas Laxström <niklas.laxstrom@gmail.com>
Sun, 16 Sep 2012 20:22:22 +0000 (20:22 +0000)
rails-i18n has the same, lets see if this is flexible
enough or whether we need to allow more complex expressions.

Change-Id: I50eb0c6d1c02ca936848d310de625ed1fe43d91a

languages/Language.php
tests/phpunit/languages/LanguageTest.php

index 40d1f36..c315763 100644 (file)
@@ -3409,6 +3409,18 @@ class Language {
                if ( !count( $forms ) ) {
                        return '';
                }
+
+               // Handle explicit 0= and 1= forms
+               foreach ( $forms as $index => $form ) {
+                       if ( isset( $form[1] ) && $form[1] === '=' ) {
+                               if ( $form[0] === (string) $count ) {
+                                       return substr( $form, 2 );
+                               }
+                               unset( $forms[$index] );
+                       }
+               }
+               $forms = array_values( $forms );
+
                $pluralForm = $this->getPluralForm( $count );
                $pluralForm = min( $pluralForm, count( $forms ) - 1 );
                return $forms[$pluralForm];
index 7025449..bfb45c7 100644 (file)
@@ -1065,5 +1065,30 @@ class LanguageTest extends MediaWikiTestCase {
                        array( 10000, 'MMMMMMMMMM' ),
                );
        }
+
+       /**
+        * @dataProvider providePluralData
+        */
+       function testConvertPlural( $expected, $number, $forms ) {
+               $chosen = $this->lang->convertPlural( $number, $forms );
+               $this->assertEquals( $expected, $chosen );
+       }
+
+       function providePluralData() {
+               return array(
+                       array( 'explicit zero', 0, array(
+                               '0=explicit zero', 'singular', 'plural'
+                       ) ),
+                       array( 'explicit one', 1, array(
+                               'singular', 'plural', '1=explicit one',
+                       ) ),
+                       array( 'singular', 1, array(
+                               'singular', 'plural', '0=explicit zero',
+                       ) ),
+                       array( 'plural', 3, array(
+                               '0=explicit zero', '1=explicit one', 'singular', 'plural'
+                       ) ),
+               );
+       }
 }